package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author : Sonic
 * @version : 1.0
 * @createTime : 2024-11-30  10:01
 * @description : dish Api
 */
@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api("Dish Api")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新建菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("add dish")
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("add dish {}", dishDTO);
        dishService.saveWithFlavor(dishDTO);
        // 插入后需要清理redis缓存信息，保持数据一致性
//        redisTemplate.delete("dish" + dishDTO.getCategoryId());
        cleanCache("dish" + dishDTO.getCategoryId());

        return Result.success();
    }

    /**
     * @param: [dishPageQueryDTO]
     * @return: com.sky.result.Result<com.sky.result.PageResult>
     * @description: 分页查询
     **/
    @GetMapping("/page")
    @ApiOperation("paging query dish")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("###### paging query dish:{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * @param: [ids]
     * @return: com.sky.result.Result
     * @description: 批量删除
     **/
    @DeleteMapping
    @ApiOperation("delete dish in batches")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("###### delete dish in batches:{}", ids);
        dishService.deleteBatch(ids);
        // 删除后需要清理redis缓存信息，因为delete可能是删除多个，所以redis干脆全都删了
//        Set keys = redisTemplate.keys("dish_*");
//        redisTemplate.delete(keys);
        cleanCache("dish_*");

        return Result.success();
    }

    /**
     * @param: [id]
     * @return: com.sky.result.Result<com.sky.vo.DishVO>
     * @description: 根据id获取VO实体类(多了口味表)
     **/
    @GetMapping("/{id}")
    @ApiOperation("select dish information by id")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("###### select dish information by id");
        DishVO dishVo = dishService.getByIdWithFlavor(id);
        return Result.success(dishVo);
    }

    /**
     * @param: [dishDTO]
     * @return: com.sky.result.Result
     * @description: 更新菜品信息
     **/
    @PutMapping
    @ApiOperation("update dish information")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("update dish information:{}", dishDTO);
        dishService.updateDishWithFlavor(dishDTO);
        // 修改后需要清理redis缓存信息，因为如果修改分类的话会涉及到两个分类的信息，所以redis干脆全都删了
        // 也可以精确删除，就是得获取分类id
//        Set keys = redisTemplate.keys("dish_*");
//        redisTemplate.delete(keys);
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * @param: [categoryId]
     * @return: com.sky.result.Result
     * @description: 根据菜品分类id查询菜品列表
     **/
    @GetMapping("/list")
    @ApiOperation("select dish information by CategoryId")
    public Result getByCategoryId(Integer categoryId) {
        log.info("###### select dish information by CategoryId");
        List<Dish> dishList = dishService.getByCategoryId(categoryId);
        return Result.success(dishList);
    }

    /**
     * @param: [status, id]
     * @return: com.sky.result.Result
     * @description: 修改菜品起售停售状态
     **/
    @PostMapping("/status/{status}")
    public Result changeStatus(@PathVariable Integer status, Long id){
        dishService.changeStatus(status, id);
        cleanCache("dish_*");

        return Result.success();
    }

    /**
     * @param: [pattern]
     * @return: void
     * @description: 根据传入的pattern匹配字符串
     **/
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
